/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tree;

/**
 *
 * @author Ej-Dý
 */
public class PrirazeniTree extends VyrazTree {

    private IdentifierTree identifier;
    private VyrazTree expression;

    public PrirazeniTree(Position start, Position end, IdentifierTree identifier, VyrazTree expression) {
        super(start, end);
        this.identifier = identifier;
        this.expression = expression;
    }

    public IdentifierTree getIdentifier() {
        return identifier;
    }

    public VyrazTree getExpression() {
        return expression;
    }

    public void setExpression(VyrazTree expression) {
        this.expression = expression;
    }

    @Override
    public void accept(TreeVisitor visitor) {
        visitor.visitPrirazeni(this);
    }

    @Override
    public void generate(Context ctx) {
        if (expression instanceof DeallocTree) {
        } else {
            expression.generate(ctx);
        }
        if (identifier instanceof ArrayTree) {
            identifier.generate(ctx);
        }
        switch (identifier.getType()) {
            case INTEGER:
            case STRING:
                //ctx.print("i");
                break;
            case REAL:
                ctx.print("d");
                break;
            case ARRAY:
                ctx.print("a");
                break;
            case POINTER:
                //ctx.print("i");
                break;
            case CONV:
                //ctx.print("s");
                break;
        }
        if (expression instanceof DeallocTree) {
            ctx.println("load " + identifier.getSlot());
            expression.generate(ctx);
        } else {
            ctx.println("store " + identifier.getSlot());
        }

    }

    @Override
    public String toString() {
        return String.format("Prirazeni %s: [%s, %s]", super.toString(), identifier, expression);
    }

    @Override
    public Type getType() {
        return expression.getType();
    }

    @Override
    public void setType(Type t) {
        expression.setType(t);
    }
}
